package com.example.food.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.example.home.R;
import com.example.home.tool.GeneralClass;
import com.example.home.tool.OKHttpTool;
import com.google.gson.Gson;

import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Field;
import java.util.Map;

public class PayOrderActivity extends AppCompatActivity {

    private ImageView img;
    private RadioGroup ra;
    private TextView money;
    private TextView submit;
    private Gson gson;
    private String orderNo;
    private String orderMoney;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pay_order);

        initView();
        payFrom();
    }

    private void payFrom() {
        ra.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                String typeId = "";
                int checkedRadioButtonId = group.getCheckedRadioButtonId();
                if (checkedRadioButtonId == R.id.ra1) {
                    typeId = "电子钱包";
                } else if (checkedRadioButtonId == R.id.ra2) {
                    typeId = "微信";
                } else if (checkedRadioButtonId == R.id.ra3) {
                    typeId = "支付宝";
                }
                //点击支付
                payOrder(typeId);
            }
        });
    }

    private void payOrder(String typeId) {
        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                JSONObject jsonObject = new JSONObject();
                try {
                    jsonObject.put("orderNo",orderNo);
                    jsonObject.put("paymentType",typeId);
                    String jsonData = jsonObject.toString();
                    OKHttpTool.doNetRequest("prod-api/api/takeout/pay",
                            OKHttpTool.POST,
                            GeneralClass.class,
                            new OKHttpTool.NetCallback() {
                                @Override
                                public void onSuccess(Object obj) {
                                    GeneralClass generalClass = (GeneralClass) obj;
                                    runOnUiThread(new Runnable() {
                                        @Override
                                        public void run() {
                                            if (generalClass.getCode() == 200){
                                                Toast.makeText(PayOrderActivity.this, "支付成功", Toast.LENGTH_SHORT).show();
                                                finishAllActivity();
                                            }
                                        }
                                    });
                                }

                                @Override
                                public void onFailure(String msg) {

                                }
                            },jsonData);
                } catch (JSONException e) {
                    e.printStackTrace();
                }

            }
        });
    }

    private void finishAllActivity() {
        //传消息回去清空数据
        Intent intent = new Intent("bov_action5");
        PayOrderActivity.this.sendBroadcast(intent);
        //结束三个Activity
        Class activityThreadClass = null;

        try {
            activityThreadClass = Class.forName("android.app.ActivityThread");
            Object activityThread = activityThreadClass.getMethod("currentActivityThread").invoke(null);
            Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
            activitiesField.setAccessible(true);
            Map activities = (Map) activitiesField.get(activityThread);
            int i = 0;
            for (Object activityRecord : activities.values()) {
                Class activityRecordClass = activityRecord.getClass();
                Field activityField = activityRecordClass.getDeclaredField("activity");
                activityField.setAccessible(true);
                Activity activity = (Activity) activityField.get(activityRecord);

                if(activity instanceof ProductsettlementActivity){
                    activity.finish();
                }else if(activity instanceof StoreDetailActivity){
                    activity.finish();
                }else if(activity instanceof PayOrderActivity  ){
                    activity.finish();
                }

                Log.e("activityThreadClass", "index:" + i + ",sum:" + activities.size()+ ", class name:" + activity.getClass().getName());
                i++;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private void initView() {
        Intent intent = getIntent();
        orderNo = intent.getStringExtra("OrderNo");
        orderMoney = intent.getStringExtra("orderMoney");
        img = findViewById(R.id.img);
        money = findViewById(R.id.money);
        ra = findViewById(R.id.ra);
        submit = findViewById(R.id.submit);
        gson = new Gson();
        money.setText(orderMoney);
        img.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PayOrderActivity.this.finish();
            }
        });
    }
}